#include "testanalysis.h"
#include "ui_testanalysis.h"

TestAnalysis::TestAnalysis(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TestAnalysis)
{
    ui->setupUi(this);
    ui->lblRight->setHidden(true);
    ui->lblWrong->setHidden(true);
    //choose paper number
    ChoosePaper *paper=new ChoosePaper();
    paper->setModal(true);
    paper->move((QApplication::desktop()->width()- paper->width())/2, (QApplication::desktop()->height() - paper->height())/2);
    int retpaperID=paper->exec();
    m_TestPaperID=retpaperID;
    qDebug()<<"retpaperID"<<retpaperID;
    QString sql=QString("select ID,testid,itemanswer from testpaper_item where testpaperid=%1").arg(retpaperID);
    qDebug()<<sql;
    m_TestAnalysisQuery=new QSqlQuery(sql);
    m_TestAnalysisQuery->last();

    m_iTestAmount=m_TestAnalysisQuery->at()+1;
    qDebug()<<"m_iTestAmount"<<m_iTestAmount;
    m_bScrollBarStoped=true;
    m_TestAnalysisQuery->first();
    m_iCurrentTestpaperitemID=m_TestAnalysisQuery->value(0).toInt();
    m_iCurrentTestID=m_TestAnalysisQuery->value(1).toInt();
    this->ShowQuestion(m_iCurrentTestID);
    ui->horizontalScrollBar->setMaximum(m_iTestAmount);
    ui->lblNumber->setText(QString("%1").arg(1));

}

TestAnalysis::~TestAnalysis()
{
    delete ui;
}


void TestAnalysis::on_horizontalScrollBar_valueChanged(int i)
{
   // this->ShowQuestion(ui->horizontalScrollBar->value());
   // m_query->seek()
    ui->lblNumber->setText(QString("%1").arg(i));

   if (!m_bScrollBarStoped)
        return;

    if (m_TestAnalysisQuery->seek(i-1,false))
    {
        m_iCurrentTestpaperitemID=m_TestAnalysisQuery->value(0).toInt();
        m_iCurrentTestID=m_TestAnalysisQuery->value(1).toInt();
        ShowQuestion(m_iCurrentTestID);
     }
    qDebug()<<"value changed"<<m_iCurrentTestpaperitemID;
}




void TestAnalysis::on_horizontalScrollBar_sliderPressed()
{
    m_bScrollBarStoped=false;

}
void TestAnalysis::on_horizontalScrollBar_sliderReleased()
{
    m_bScrollBarStoped=true;
    int i=ui->horizontalScrollBar->value();
    on_horizontalScrollBar_valueChanged(i);
}

void TestAnalysis::ShowQuestion(int ShowID)
{
    QSqlQuery query(QString("SELECT ID,QUESTION,OPTIONS,ANSWER,subjectID,teststyleID from test WHERE ID=%1")
                            .arg(ShowID));
    //qDebug()<<"qestionid"<<QuestionID;
    if(!query.exec())
    {
    qDebug()<<"[load error]";
    }
    else
    {
        if (query.next())
        {
                  QByteArray byteQuestion = query.value(1).toByteArray();
                  forceQuestion = new PushDoc(this);
                  OTestQuestion = new OOReader(byteQuestion);
                  OTestQuestion->moveToThread(forceQuestion);
                  connect(forceQuestion, SIGNAL(started()),OTestQuestion,SLOT(readbuffer()));
                  connect(OTestQuestion, SIGNAL(ready()), this, SLOT(drawDoc()));
                  //connect(Ooo, SIGNAL(statusRead(int,int)), this, SLOT(onRead(int,int)));
                  forceQuestion->start();


                 ///////////////////////////////////////////////////////////
                  QByteArray byteOptions = query.value(2).toByteArray();
                  forceOptions = new PushDoc(this);
                  OTestOoptions = new OOReader(byteOptions);
                  OTestOoptions->moveToThread(forceOptions);
                  connect(forceOptions, SIGNAL(started()),OTestOoptions,SLOT(readbuffer()));
                  connect(OTestOoptions, SIGNAL(ready()), this, SLOT(drawOptions()));
                  //connect(Ooo, SIGNAL(statusRead(int,int)), this, SLOT(onRead(int,int)));
                  forceOptions->start();


                  int iStandardAnswer=query.value(3).toInt();
                  this->ShowItemAnswer(iStandardAnswer);

            }
    }
}

void TestAnalysis::drawDoc()
{
    /* remote image is loading after ..... */
    if (OTestQuestion) {
        ui->textEdit->setDocument ( OTestQuestion->document()->clone() );

        delete OTestQuestion;
        }

        if (forceQuestion) {
            forceQuestion->quit();
        }
}

void TestAnalysis::drawOptions()
{
    /* remote image is loading after ..... */
    if (OTestOoptions) {
        ui->textEditOptions->setDocument ( OTestOoptions->document()->clone() );
        delete OTestOoptions;
        }

        if (forceOptions) {
            forceOptions->quit();
            ui->horizontalScrollBar->setEnabled(true);
        }
}

void TestAnalysis::ShowItemAnswer(int iStandardAnswer)
{
    if (iStandardAnswer==0)
        ui->lblRightAnswer->setText(QString(tr("标准答案未设置")));
    else
        ui->lblRightAnswer->setText(QString(tr("标准答案是:%1")).arg(ConvertAnswer(iStandardAnswer)));

    //ui->lineEdit->setText(QString(tr("正确 answer %1")).arg(1));
    QSqlQuery query(QString("select itemanswer from testpaper_item WHERE ID=%1")
                                            .arg(m_iCurrentTestpaperitemID));
    query.first();
    int iItemanswer=query.value(0).toInt();
    if (iItemanswer==0)
        ui->lineEdit->setText(QString(tr("您的答案未填")));
    else
        ui->lineEdit->setText(QString(tr("您的答案是:%1")).arg(ConvertAnswer(iItemanswer)));


    if(iStandardAnswer==iItemanswer and iStandardAnswer>0)
        {
            ui->lblRight->setHidden(false);
            ui->lblWrong->setHidden(true);
          }
    else
        {
            ui->lblRight->setHidden(true);
            ui->lblWrong->setHidden(false);
        }

   /*
   //---显示正确与错误的图片
        QPixmap pixmapBg(":/testanalysis/wrong.jpg");
        QPainter painter(this);
        painter.drawPixmap(0, 0, pixmapBg);
        QPixmap pixmapTop(":/testanalysis/right.jpg");
        int alpha = 125;
    //---前景图片透明度处理
        QPixmap temp(pixmapTop.size());
        temp.fill(Qt::transparent);
        QPainter p(&temp);
        p.setCompositionMode(QPainter::CompositionMode_Source);
        p.drawPixmap(0, 0, pixmapTop);
        p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
        p.fillRect(temp.rect(), QColor(0, 0, 0, alpha));
    //--lable显示前景图片
        ui->lblRight->setScaledContents(true);
        ui->lblRight->setPixmap(temp);
*/

}
QString TestAnalysis::ConvertAnswer(int iAnswer)
{
    QString str;
    //str='';
    switch(iAnswer)
    {
        case 1:str='A';break;
        case 2:str='B';break;
        case 3:str='C';break;
        case 4:str='D';break;
        case 5:str='E';break;
    default:
         str='Z';break;
    }
    return str;
}
